#!/bin/sh
# Copyright (c) 2005-2007  WyldRyde IRC Network
# All rights reserved.
#
# $Id$
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

for script in `pwd`/include/*
do
	. ${script}
done

conf_init

WORKDIR=`pwd`
COMMAND=`basename $0`

check_config() {
	if [ "${1}" = "" ] ; then
		# using conf/network.conf to write to
		CONFFILE="${WORKDIR}/conf/network.conf"
	else
		CONFFILE="${WORKDIR}/conf/${1}"
		if [ ! -e ${CONFFILE} ] ; then
			echo "ERROR: ${WORKDIR}/conf/${1} doesn't exist, aborting"
			exit
		fi
	fi
	echo ${CONFFILE}
}

add_server() {
	check_config $1
}

create_config() {
	if [ "${1}" = "" ] ; then
		if [ "${CONFFILE}" = "" ] ; then
			echo "ERROR: must specify a config file!"
			exit
		fi
	else
		CONFFILE="${WORKDIR}/conf/${1}"
		if [ -e ${CONFFILE} ] ; then
			echo "ERROR: config file already exists!"
			exit
		fi
	fi

	
}
prime_server() {
        ossh ${1} 'rm -rf data/ && mkdir -p data/logs/ && mkdir data/wr-ircd/ && touch data/logs/unreal.log && touch data/local.conf'
        cd ${WORKDIR}/certs/ && tar -cpvf - *.pem | bzip2 | ossh ${1} 'bunzip2 | tar -xpf - -C ./data'
}
push_key() {
        ossh ${1} 'if [ ! -d ~/.ssh/ ]; then mkdir ~/.ssh/; fi; chmod -R 0700 ~/.ssh/'
        cat ~/.ssh/id_rsa.pub | ossh ${1} 'cat >> ~/.ssh/authorized_keys'
}
make_modules() {
        ossh ${1} 'cd wr-ircd && if [ -f makemodules ]; then chmod +x makemodules; ./makemodules; fi'
}
push_bins() {
	cd /tmp/svn && tar -cpvf - wr-ircd | bzip2 | ossh ${1} 'bunzip2 | tar -xpf - -C ./'
	ossh ${1} 'cd wr-ircd && ./configure --enable-hub  --enable-ssl --with-showlistmodes --with-listen=5 --with-dpath=`pwd`/wr-ircd  --with-spath=`pwd`/wr-ircd/src/ircd --with-nick-history=2000 --with-sendq=3000000 --with-bufferpool=18 --with-hostname=127.0.0.1 --with-permissions=0600 --enable-dynamic-linking'
	ossh ${1} 'cd wr-ircd && cd . && make clean && make'
	mkdir -p /tmp/binbackup/
	ossh ${1} 'tar -cpf - wr-ircd | bzip2' > /tmp/binbackup/${1}.tar.bz2
}
push_server() {
	rm -rf /tmp/svn
	mkdir -p /tmp/svn
	cd /tmp/svn
	svn checkout http://wr-ircd.googlecode.com/svn/trunk/ wr-ircd
	if [ "${1}" = "" ] ; then
		for WORKINGSERVERS in `grep ^S ${STRIPCONF}`
		do
			SERVER=`echo ${WORKINGSERVERS} | cut -d : -f 2`
			push_bins ${SERVER}
		done
	else
		WORKINGSERVERS=`grep ^S ${STRIPCONF} | grep ${1}`
		SERVER=`echo ${WORKINGSERVERS} | cut -d : -f 2`
		push_bins ${SERVER}
	fi
}
backup_module() {
	ossh ${1} tar -cpf - ${2} >${BACKUPPATH}/${1}/${2}.tar 2>/dev/null
}
backup_sys() {
	BACKUPPATH=${HOME}/back/$(rand)
	mkdir -p ${BACKUPPATH}
	DATE=`date +%Y%m%d%H%M`
	echo "${DATE}=${BACKUPPATH}" >> ${HOME}/back/back.log
        for WORKINGSERVERS in `grep ^S ${STRIPCONF}`
        do
            SERVER=`echo ${WORKINGSERVERS} | cut -d : -f 2`
	    mkdir -p ${BACKUPPATH}/${SERVER}
            backup_module ${SERVER} wr-ircd
            backup_module ${SERVER} wr-area51
            backup_module ${SERVER} .ssh
            backup_module ${SERVER} bopm
            backup_module ${SERVER} services
            backup_module ${SERVER} data
            backup_module ${SERVER} Unreal3.2.x
            backup_module ${SERVER} unreal
        done
	tar -cf ${BACKUPPATH}/home.tar -x back /home/wamphyre/* /home/wamphyre/.*
	tar -cf ${BACKUPPATH}/local.tar /usr/local/*
	tar -cf ${HOME}/back/${DATE}.tar ${BACKUPPATH}/*
	bzip2 ${HOME}/back/${DATE}.tar

}
ping() {
	for WORKINGSERVERS in `grep ^S ${STRIPCONF}`
	do
		SERVER=`echo ${WORKINGSERVERS} | cut -d : -f 2`
		echo "${SERVER}: `ossh ${SERVER} uname -sr 2>/dev/null` `ossh ${SERVER} openssl version 2>/dev/null` `ossh ${SERVER} gcc --version 2>/dev/null | head -n 1 ` `ossh ${SERVER} 'grep MAXCONNECTIONS wr-ircd/config.log | head -n 1'` "

done
}
dump_list() {
	for WORKINGSERVERS in `grep ^S ${STRIPCONF}`
	do
		SERVER=`echo ${WORKINGSERVERS} | cut -d : -f 2`
		echo -e ${SERVER}
		for USERS in `grep ^U ${STRIPCONF}`
		do
			case `echo ${USERS} | cut -d : -f 5` in
				*g*)
					DROPIT=y	
				;;
				*r*)
					DROPIT=y
				;;
				*)
					DROPIT=n
				;;
			esac
			if [ "${DROPIT}" != "y" ] ; then
				for servern in `echo ${USERS} | cut -d : -f 6 | sed s/\-/\ /g`
				do
					if [ "` echo ${servern} | cut -d @ -f 2`" = "${SERVER}" ] ; then
						echo -e "\t`echo ${servern} | cut -d @ -f 1`=`echo ${USERS} | cut -d : -f 3 | sed s/\_/\ /g` <`echo ${USERS} | cut -d : -f 2`>"
					fi
				done
			fi
		done
        done
	
}
rehash_list() {
	for WORKINGSERVERS in `grep ^S ${STRIPCONF}`
	do
		SERVER=`echo ${WORKINGSERVERS} | cut -d : -f 2`
		echo ${SERVER}
		ossh ${SERVER} killall -1 ircd 2>/dev/null
	done
}

add_server() {
	ossh ${1} mkdir -p data/wr-ircd
	ossh ${1} mkdir -p wr-ircd
}
usage() {
	echo "usage:"
	echo -e "\t${COMMAND} push"
	echo -e "\t${COMMAND} create"
	echo -e "\t${COMMAND} add"
	echo -e "\t${COMMAND} backup"
	echo -e "\t${COMMAND} ping"
	echo -e "\t${COMMAND} dump"
	echo -e "\t${COMMAND} rehash"
        echo -e "\t${COMMAND} prime"
        echo -e "\t${COMMAND} prime"
        echo -e "\t${COMMAND} makemodules"
}



case "${1}" in
	[pP][uU][sS][hH]) 
		check_config
		push_server $2
	;;
        [pP][uU][sS][hH][kK][eE][yY])
                push_key $2
        ;;
	[aA][dD][dD])
		add_server $2
	;;
	[cC][rR][eE][aA][tT][eE])
		create_config $2
	;;
	[bB][aA][cC][kK][uU][pP])
		backup_sys $2
	;;
	[pP][iI][Nn][Gg])
		ping $2
	;;
	[dD][uU][mM][pP])
		dump_list
	;;
        [pP][rR][iI][mM][eE])
                prime_server $2
        ;;
        [mM][aA][kK][eE][mM][oO][dD][uU][lL][eE][sS])
                make_modules $2
        ;;
	[rR][eE][hH][aA][sS][hH])
		rehash_list
	;;
	*)
		usage
	;;
esac

rm -r ${OUTPUTPATH}
